package com.tledu.car.controller;

import com.tledu.carUse.domain.CarUse;
import com.tledu.carUse.domain.dto.CarUseDto;
import com.tledu.carUse.domain.vo.CarUseVo;
import com.tledu.carUse.service.ICarUseService;
import com.tledu.common.annotation.Log;
import com.tledu.common.core.controller.BaseController;
import com.tledu.common.core.domain.AjaxResult;
import com.tledu.common.core.page.TableDataInfo;
import com.tledu.common.enums.BusinessType;
import com.tledu.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 我的用车Controller
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@RestController
@RequestMapping("/car/use")
public class CarUseController extends BaseController {
    @Autowired
    private ICarUseService carUseService;
    @Resource
    private CarUseVo carUseVo;
    /**
     * 查询我的用车列表
     */
    @PreAuthorize("@ss.hasPermi('car:use:list')")
    @GetMapping("/list")
    public TableDataInfo list(CarUseDto carUseDto) {
        startPage();
        List<CarUse> list = carUseService.selectCarUseList(carUseDto);
        return getDataTable(list);
    }

    /**
     * 导出我的用车列表
     */
    @PreAuthorize("@ss.hasPermi('car:use:export')")
    @Log(title = "我的用车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarUseDto carUseDto) {
        List<CarUse> list = carUseService.selectCarUseList(carUseDto);
        ExcelUtil<CarUse> util = new ExcelUtil<CarUse>(CarUse.class);
        util.exportExcel(response, list, "我的用车数据");
    }

    /**
     * 获取我的用车详细信息
     */
    @PreAuthorize("@ss.hasPermi('car:use:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(carUseService.selectCarUseById(id));
    }


    /**
     * 获取我的用车详细信息
     */
    @PreAuthorize("@ss.hasPermi('car:use:query')")
    @GetMapping(value = "/getInfo1")
    public AjaxResult getInfo1(@RequestBody CarUseVo carUseVo) {
        return success(carUseService.selectCarUseBycarId(carUseVo));
    }

    /**
     * 新增我的用车
     */
    @PreAuthorize("@ss.hasPermi('car:use:add')")
    @Log(title = "我的用车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarUse carUse) {
        return toAjax(carUseService.insertCarUse(carUse));
    }

    /**
     * 修改我的用车
     */
    @PreAuthorize("@ss.hasPermi('car:use:edit')")
    @Log(title = "我的用车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarUse carUse) {
        return toAjax(carUseService.updateCarUse(carUse));
    }

    /**
     * 移除我的用车
     */
    @PreAuthorize("@ss.hasPermi('car:use:remove')")
    @Log(title = "我的用车", businessType = BusinessType.UPDATE)
    @PutMapping("/remove/{id}")
    public AjaxResult edit(@PathVariable long id) {
        return toAjax(carUseService.removeCarUse(id));
    }


    /**
     * 删除我的用车
     */
    @PreAuthorize("@ss.hasPermi('car:use:remove')")
    @Log(title = "我的用车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(carUseService.deleteCarUseByIds(ids));
    }
}
